

function $(str) {
	return document.getElementById(str);
}

var reg = $("reg");
var source = $("source");
var result = $("result");
var regVal, srcVal, resVal, regExp;
/**
 * 初始化参数
 */
function _initValue() {
	regVal = reg.value;
	srcVal = source.value;

}
/**
 * 处理结果html
 */
function _excl() {

	regExp = new RegExp(regVal, "g");
	var res = regExp.exec(srcVal);
	result.innerHTML = _createResHtml(res, srcVal);
	console.dir(res);
};
function _createResHtml(res, srcVal) {
	if (!res) {
		return srcVal;
	}
	var temp = "";
	var color = "red";
	var srcs = srcVal.split(regExp);
	for (var i = 0, n = srcs.length; i < n; i++) {
		if (i % 2 === 0) {
			color = "red";
		} else {
			color = "blue";
		}
		temp += srcs[i] + "<span style=\"background-color:" + color + ";\">"
				+ res[0] + "</span>";
	}
	console.log(temp);
	return temp;

}
/**
 * 开始生成结果
 */
function star() {
	_initValue();
	_excl(regExp);

}
reg.addEventListener("change", star);
reg.addEventListener("keydown", function(event) {
			if (event.keyCode == 13) {
				star();
			}
		});
